#!/usr/bin/env node
//有效避免on事件名称书写错误检测不到
const EventEmitter = require('events').EventEmitter;

function MusicPlayer(track) {
    this.track = track;
    this.playing = false;
    this.events = {
        play:'play',
        stop:'stop'
    }

    for(const methodName in EventEmitter.prototype) {
        console.log(methodName);
        this[methodName] = EventEmitter.prototype[methodName];
    }
}

MusicPlayer.prototype = {
    toString: function() {
        if(this.playing) {
            return "Now Playing: " + this.track;
        }else {
            return 'Stopped' ;
        }
    } 
};

const musicPlayer = new MusicPlayer('storm');

const e = musicPlayer.events;

musicPlayer.on(e.play, function() {
    this.playing =  true;
    console.log('\n', true.toString());
});

musicPlayer.on(e.stop, function() {
    this.playing = false;
    console.log('\n', this.toString());
});

musicPlayer.emit(e.play);

setTimeout(function() {
    musicPlayer.emit(e.stop);
},2000);

